package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.TTaskJobDao;
import com.ruoyi.system.domain.dto.TTaskJobDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.TTaskJobVo;
import com.ruoyi.system.mapper.TTaskJobExtendMapper;
import com.ruoyi.system.mapper.TTaskJobMapper;
import com.ruoyi.system.mapper.TTaskLogMapper;
import com.ruoyi.system.service.TTaskJobService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author nanyang
 */
@Service
public class TTaskJobServiceImpl implements TTaskJobService {
    
    @Resource
    private TTaskJobMapper taskJobMapper;
    
    @Resource
    TTaskJobExtendMapper extendMapper;

    @Resource
    TTaskLogMapper taskLogMapper;

    @Resource
    TTaskJobDao taskJobDao;
    
    @Override
    public Integer insertTaskJob(TTaskJobDto.Insert taskJobInsert) {
        // 新增实体数据
        TTaskJob taskJob = BeanUtil.copyProperties(taskJobInsert, TTaskJob.class);
        taskJob.setId(IdUtil.getSnowflake().nextId());
        taskJob.setCreateBy(SecurityUtils.getUsername());
        taskJob.setCreateTime(new Date());
        return taskJobMapper.insertSelective(taskJob);
    }

    @Override
    public Integer updateTaskJob(TTaskJobDto.Update taskJobUpdate) {
        // 修改实体
        TTaskJob taskJob = BeanUtil.copyProperties(taskJobUpdate, TTaskJob.class);
        taskJob.setUpdateBy(SecurityUtils.getUsername());
        taskJob.setUpdateTime(new Date());
        return taskJobMapper.updateByPrimaryKeySelective(taskJob);
    }

    @Override
    public Integer deleteTaskJob(TTaskJobDto.Delete taskJobDelete) {
        TTaskJobExample interfaceExample = new TTaskJobExample();
        interfaceExample.createCriteria().andIdIn(taskJobDelete.getIdList());
        TTaskJob taskJob = new TTaskJob();
        taskJob.setDelFlag((byte) 2);
        return taskJobMapper.updateByExampleSelective(taskJob, interfaceExample);
    }

    @Override
    public List<TTaskJobVo> listTaskJob(TTaskJobDto.Query queryTaskJob) {
        TTaskJobExample taskJobExample = new TTaskJobExample();
        TTaskJobExample.Criteria criteria = taskJobExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (queryTaskJob.getJobType() != null) {
            criteria.andJobTypeEqualTo(queryTaskJob.getJobType());
        }
        if (queryTaskJob.getMisfirePolicy() != null) {
            criteria.andMisfirePolicyEqualTo(queryTaskJob.getMisfirePolicy());
        }
        if (queryTaskJob.getConcurrent() != null) {
            criteria.andConcurrentEqualTo(queryTaskJob.getConcurrent());
        }
        if (queryTaskJob.getStatus() != null) {
            criteria.andStatusEqualTo(queryTaskJob.getStatus());
        }
        taskJobExample.setOrderByClause("create_time desc");
        return taskJobDao.selectByExample(taskJobExample);
    }

    @Override
    public Integer insertExtend(TTaskJobDto.ExtendInsert extendInsert) {
        // 新增实体数据
        TTaskJobExtend taskJobExtend = BeanUtil.copyProperties(extendInsert, TTaskJobExtend.class);
        taskJobExtend.setId(IdUtil.getSnowflake().nextId());
        taskJobExtend.setCreateBy(SecurityUtils.getUsername());
        taskJobExtend.setCreateTime(new Date());
        return extendMapper.insertSelective(taskJobExtend);
    }

    @Override
    public Integer updateExtend(TTaskJobDto.ExtendUpdate extendUpdate) {
        // 修改实体
        TTaskJobExtend taskJobExtend = BeanUtil.copyProperties(extendUpdate, TTaskJobExtend.class);
        taskJobExtend.setUpdateBy(SecurityUtils.getUsername());
        taskJobExtend.setUpdateTime(new Date());
        return extendMapper.updateByPrimaryKeySelective(taskJobExtend);
    }

    @Override
    public Integer deleteExtend(TTaskJobDto.ExtendDelete extendDelete) {
        TTaskJobExtendExample extendExample = new TTaskJobExtendExample();
        extendExample.createCriteria().andIdIn(extendDelete.getIdList());
        TTaskJobExtend taskJobExtend = new TTaskJobExtend();
        taskJobExtend.setDelFlag((byte) 2);
        return extendMapper.updateByExampleSelective(taskJobExtend, extendExample);
    }

    @Override
    public List<TTaskLog> queryLog(TTaskJobDto.LogQuery logQuery) {
        TTaskLogExample logExample = new TTaskLogExample();
        TTaskLogExample.Criteria criteria = logExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (logQuery.getJobType() != null) {
            criteria.andJobTypeEqualTo(logQuery.getJobType());
        }
        if (StrUtil.isNotEmpty(logQuery.getAppId())) {
            criteria.andAppIdEqualTo(logQuery.getAppId());
        }
        if (StrUtil.isNotEmpty(logQuery.getName())) {
            criteria.andAppIdLike("%" + logQuery.getAppId() + "%");
        }
        if (logQuery.getStatus() != null) {
            criteria.andStatusEqualTo(logQuery.getStatus());
        }
        if (logQuery.getStartTime() != null) {
            criteria.andTimeGreaterThanOrEqualTo(logQuery.getStartTime());
        }
        if (logQuery.getEndTime() != null) {
            criteria.andTimeLessThanOrEqualTo(logQuery.getEndTime());
        }
        return taskLogMapper.selectByExample(logExample);
    }
}
